package cc.angis.xiugallery.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import cc.angis.xiugallery.data.PostData;

public class PostDBHelper extends BaseDBHelper {
	
	public PostDBHelper(){
		
	}
	
	public PostDBHelper(Context context, String table){
		super(context, table);
	}
	
	public PostDBHelper(Context context, String table, ContentValues values){
		super(context, table, values);
	}
	
	public long delete(int id){
		mWhereClaus = "id=?";
		mWhereArgs = new String[] { String.valueOf(id) };
		return delDB();
	}
	
	public long delete(long imgid){
		mWhereClaus = "imgid=?";
		mWhereArgs = new String[] { String.valueOf(imgid) };
		return delDB();
	}
	
	public synchronized long update(String postid, int commentcount, long lastupdate){
		mWhereClaus = "postid=?";
		mWhereArgs = new String[] { postid };
		mValues = new ContentValues();
		if (commentcount != -1)
			mValues.put(TableConstants.PostColumn.COMMENT_COUNT, commentcount);
		mValues.put(TableConstants.PostColumn.LASTUPDATE, lastupdate);
		return updateDB();
	}
	
	public long insert(long imgid, String postid, String content, long posttime, int commentcount){
		mValues = new ContentValues();
		mValues.put(TableConstants.PostColumn.IMGID, imgid);
		mValues.put(TableConstants.PostColumn.POSTID, postid);
		mValues.put(TableConstants.PostColumn.CONTENT, content);
		mValues.put(TableConstants.PostColumn.POST_TIME, posttime);
		mValues.put(TableConstants.PostColumn.COMMENT_COUNT, commentcount);
		mValues.put(TableConstants.PostColumn.LASTUPDATE, posttime);
		return insertDB();
	}
	
	public List<PostData> getAllPosts(){
		List<PostData> postDatas = new ArrayList<PostData>();
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, null, null, null, null, null, TableConstants.PostColumn.LASTUPDATE + " DESC");
		int idColumn = cursor.getColumnIndex(
				TableConstants.PostColumn.ID);
		int postidColumn = cursor.getColumnIndex(
				TableConstants.PostColumn.POSTID);
		int imgidColumn = cursor.getColumnIndex(
				TableConstants.PostColumn.IMGID);
		int contentColumn = cursor.getColumnIndex(
				TableConstants.PostColumn.CONTENT);
		int timeColumn = cursor.getColumnIndex(
				TableConstants.PostColumn.POST_TIME);
		int countColumn = cursor.getColumnIndex(
				TableConstants.PostColumn.COMMENT_COUNT);
		if (cursor.moveToFirst()){
			PostData postData;
			do {
				postData = new PostData();
				postData.setId(cursor.getInt(idColumn));
				postData.setPostId(cursor.getString(postidColumn));
				postData.setImgId(cursor.getLong(imgidColumn));
				postData.setContent(cursor.getString(contentColumn));
				postData.setPostTime(cursor.getLong(timeColumn));
				postData.setCommentCount(cursor.getInt(countColumn));
				postDatas.add(postData);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return postDatas;
	}
	
	public long getPostLastUpdateByPostId(String postId){
		long postLastUpdate = -1;
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, new String[]{TableConstants.PostColumn.LASTUPDATE}, "postid=?", 
				new String[]{postId}, null, null, null);
		int updateColumn = cursor.getColumnIndex(
				TableConstants.PostColumn.LASTUPDATE);
		if (cursor.moveToFirst()){
			do {
				postLastUpdate = cursor.getLong(updateColumn);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return postLastUpdate;
	}
	
}
